package cn.fl.settlement.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.condition.CapCstStmScheduleQC;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.vo.PrjFileInfoVO;
import cn.fl.settlement.service.ISettlementFileService;
import cn.fl.settlement.vo.CapCstStmScheduleVO;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysUlFilesRecService;
import cn.fl.system.vo.SysUlFilesRecVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.BackingStoreException;

/**
* @Description:    结清附件管理Controller
* @Author:         liurong
* @CreateDate:     2020年9月2日18:10:53
* @Version:        1.0
*/
@Controller
@RequestMapping("/settlement/file")
public class SettleFileController extends BaseController {

    @Autowired
    private ISettlementFileService settlementFileService;

    @Autowired
    private ISysUlFilesRecService sysUlFilesRecService;


    @RequestMapping(value = "/loadList.do", method = { RequestMethod.GET})
    @ResponseBody
    public Object loadList(HttpServletRequest request) throws BaseException {
        try {
            String pkId = request.getParameter("pkId");
            if (StringUtils.isEmpty(pkId)) {
                throw new BaseException("数据主键ID不能为空！");
            }

            List<PrjFileInfoVO> fileList = settlementFileService.searchSettleFileList(Long.parseLong(pkId));
            if (CollectionUtils.isNotEmpty(fileList)) {
                return new ListPageVO<PrjFileInfoVO>(fileList, fileList.size());
            }
            return new ListPageVO<PrjFileInfoVO>(new ArrayList<>(), 0);
        } catch (Exception e) {
            BaseLogUtils.newWebLogger("查询结清附件数据异常！").setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        }
    }

    @RequestMapping(value = "/saveFile.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object uploadCstFile(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_FILE_UPLOAD_SUCC);
        String fileParams = request.getParameter("fileParams");
        String cfgCd = request.getParameter("cfgCd");
        String cfgDtlCd = request.getParameter("cfgDtlCd");
        String orgCd = request.getParameter("orgCd");
        String pkId = request.getParameter("pkId");
        try {
            if (StringUtils.isEmpty(pkId)) {
                throw new BaseException("文件上传失败！数据主键不能为空！");
            } else {
                boolean flag = this.sysUlFilesRecService.addAttchmnt(pkId, fileParams, cfgCd, cfgDtlCd, "PRJ_PRD_PAY_SCH_M", null, null, null, false, orgCd);
                if (!flag) {
                    throw new BaseException("文件上传失败！");
                }
            }
        } catch (Exception e) {
            BaseLogUtils.newWebLogger("上传结清文件失败！==>pkId=" + pkId + ",cfgCd=" + cfgCd + ",cfgDtlCd=" + cfgDtlCd + ",file=" + fileParams).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_FILE_UPLOAD_FAIL);
        }
        return result;
    }

    @RequestMapping(value = "/removeFile.do", method = {RequestMethod.POST})
    @ResponseBody
    public DataResultVO removeFile(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
        String fileId = request.getParameter("fileId");
        try {
            if (StringUtils.isEmpty(fileId)) {
                throw new BaseException("文件主键ID不能为空！");
            }
            boolean success = settlementFileService.removeFile(Long.parseLong(fileId));
            if (!success) {
                throw new BaseException("文件删除失败！");
            }
        } catch (Exception e) {
            BaseLogUtils.newWebLogger("删除结清文件失败！==>fileId=" + fileId).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
        }
        return result;
    }




}

